
/**
 * 穿山甲模块
 */
export default class CSJADUtils {
    private static instance: CSJADUtils;

    public static getInstance() {
        if (!this.instance) {
            this.instance = new CSJADUtils();
        }
        return this.instance;
    }

    private mZyPangolinAd: ApicloudModules['zyPangolinAd'];

    private constructor() {
        this.mZyPangolinAd = api.require('zyPangolinAd')
        if (!this.mZyPangolinAd) {
            throw new Error('zyPangolinAd 模块没引入')
        }
    }

    // 激励视频播放完毕监听
    private mRewardVideoPlayAllListener?: RewardVideoPlayAllListener;


    /**
     * 设置激励视频播放完毕监听
     * @param listener 
     */
    public setRewardVideoPlayAllListener(listener: RewardVideoPlayAllListener) {
        this.mRewardVideoPlayAllListener = listener;
    }


    public init(params?: { appId: string, appName: string }) {
        this.mZyPangolinAd.init(params || {
            appId: api.systemType === 'ios' ? '5030466' : '5026553',
            appName: "小熊有好货_" + api.systemType,
        })
    }

    public requestPermissionIfNecessary(){
        this.mZyPangolinAd.requestPermissionIfNecessary()
    }

    public showBannerAd(css: CSJShowRewardVideoAdParams) {
        this.mZyPangolinAd.showBannerAd(css)
    }

    public closeBannerAd() {
        this.mZyPangolinAd.closeBannerAd()
    }

    public loadVideoAd(params: CSJLoadRewardVideoAdParams): Promise<boolean> {
        return new Promise(res=>{
            this.mZyPangolinAd.loadRewardVideoAd(params, (ret) => {
                console.log(JSON.stringify(ret))
                if(!ret.status){
                    res(false);
                    return
                }
                if (ret.result === "激励视频缓存完成") {
                    res(true)
                }else if(ret.result === "激励视频关闭"){
                    this.mRewardVideoPlayAllListener && this.mRewardVideoPlayAllListener()
                }
            })
        })
    }

    public showVideoAd(): Promise<boolean>{
        const time = api.systemType === 'ios' ? 2000 : 500
        return new Promise(res=>{
            setTimeout(()=>{
                this.mZyPangolinAd.showRewardVideoAd((ret) => {
                    res(ret.status)
                })
            },time)
        })
    }

}


interface RewardVideoPlayAllListener {
    (): void;
}
